using Oliving.Edge.Model.Models;
using Oliving.Edge.Services.BASE;
using Oliving.Edge.IServices;
using Oliving.Edge.IRepository;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using Oliving.Edge.Model;
using Oliving.Edge.Model.Dto.Base;

namespace Oliving.Edge.FrameWork.Services
{
    /// <summary>
    /// sysUserInfoServices
    /// </summary>	
    public class SysUserInfoServices : BaseServices<sysUserInfo>, ISysUserInfoServices
    {

        IsysUserInfoRepository _dal;
        IUserRoleServices _userRoleServices;
        IRoleRepository _roleRepository;
        public SysUserInfoServices(IsysUserInfoRepository dal, IUserRoleServices userRoleServices, IRoleRepository roleRepository)
        {
            this._dal = dal;
            this._userRoleServices = userRoleServices;
            this._roleRepository = roleRepository;
            base.BaseDal = dal;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="loginPwd"></param>
        /// <returns></returns>
        public async Task<sysUserInfo> SaveUserInfo(string loginName, string loginPwd)
        {
            sysUserInfo sysUserInfo = new sysUserInfo(loginName, loginPwd);
            sysUserInfo model = new sysUserInfo();
            var userList = await base.Query(a => a.uLoginName == sysUserInfo.uLoginName && a.uLoginPWD == sysUserInfo.uLoginPWD);
            if (userList.Count > 0)
            {
                model = userList.FirstOrDefault();
            }
            else
            {
                var id = await base.Add(sysUserInfo);
                model = await base.QueryById(id);
            }

            return model;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="loginPwd"></param>
        /// <returns></returns>
        public async Task<string> GetUserRoleNameStr(string loginName, string loginPwd)
        {
            //string roleName = "";
            //var user = (await base.Query(a => a.uLoginName == loginName && a.uLoginPWD == loginPwd)).FirstOrDefault();
            //var roleList = await _roleRepository.Query(a => a.IsDeleted == false);
            //if (user != null)
            //{
            //    var userRoles = await _userRoleServices.Query(ur => ur.UserId == user.uID);
            //    if (userRoles.Count > 0)
            //    {
            //        var arr = userRoles.Select(ur => ur.RoleId.ObjToString()).ToList();
            //        var roles = roleList.Where(d => arr.Contains(d.Id.ObjToString())); 
            //        roleName = string.Join(',', roles.Select(r => r.Name).ToArray());
            //    }
            //}

            List<UserLoginDto> list = await _dal.GetUserByPwd(loginName, loginPwd);

            if (list == null || list.Count == 0) { return ""; }
            else
            {
                return string.Join(',', list.Select(r => r.roles).ToArray());
            }


        }

        public async Task<List<sysUserInfo>> GetEdgePage(int pageIndex, int pageSize)
        {
            return await _dal.GetEdgePage(pageIndex, pageSize);
        }


        public async Task<PageModel<sysUserInfo>> GetEdgePageModel(int pageIndex, int pageSize)
        {
            return await _dal.GetEdgePageModel(pageIndex, pageSize);
        }

        #region 小程序登陆 获取token  白克平 2020-9-15
        /// <summary>
        /// 小程序登陆 获取token
        /// </summary>
        /// <author>白克平</author>
        /// <datetime>2020-9-15</datetime>
        /// <param name="phone">手机号</param>
        /// <returns></returns>
        public async Task<List<UserLoginDto>> XGetUserRoleNameStr(string phone)
        {

            List<UserLoginDto> list = await _dal.XGetUserByPwd(phone);
            return list;
     
        }
        #endregion







    }
}

//----------sysUserInfo结束----------
